import Vue from "vue"
import VueRouter from "vue-router"
import { Message } from 'element-ui'

Vue.use(VueRouter)

// 去除跳转异常
const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return routerPush.call(this, location).catch(error => error)
}

const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    { 
        path: '*', 
        name: 'noPage', 
        component: () => import("@/views/404/noPage")
    },
    { 
        path: '/login', 
        name: 'login', 
        component: () => import("@/views/login")
    },
    { 
        path: '/index', 
        name: 'index', 
        component: () => import("@/views/index/index"),
        children: [
            {
                path: '',
                name: 'console',
                component: () => import("@/views/index/console"),
            },
        ]
    },
    { 
        path: '/hinfo', 
        name: 'hinfo', 
        component: () => import("@/views/house/hinfo")
    },
    {
        path: '/home',
        name: 'home',
        component: () => import("@/views/home/home"),
        children: [
            {
                path: '',
                name: 'welcome',
                component: () => import("@/views/home/welcome"),
                meta: {
                    keepAlive: true,
                    title: '首页'
                },
            },
            {
                path: '/page1',
                name: 'page1',
                component: () => import("@/views/house/page1"),
                meta: {
                    keepAlive: true,
                    title: '各区楼盘'
                },
            },
            {
                path: '/page2',
                name: 'page2',
                component: () => import("@/views/house/page2"),
                meta: {
                    keepAlive: true,
                    title: '各区楼盘'
                },
            },
            {
                path: '/page3',
                name: 'page3',
                component: () => import("@/views/house/page3"),
                meta: {
                    keepAlive: true,
                    title: '各区楼盘'
                },
            },
            {
                path: '/page4',
                name: 'page4',
                component: () => import("@/views/house/page4"),
                meta: {
                    keepAlive: true,
                    title: '各区楼盘'
                },
            },
            {
                path: '/page5',
                name: 'page5',
                component: () => import("@/views/house/page5"),
                meta: {
                    keepAlive: true,
                    title: '各区楼盘'
                },
            },
            {
                path: '/page6',
                name: 'page6',
                component: () => import("@/views/house/page6"),
                meta: {
                    keepAlive: true,
                    title: '各区楼盘'
                },
            },
            {
                path: '/page7',
                name: 'page7',
                component: () => import("@/views/house/page7"),
                meta: {
                    keepAlive: true,
                    title: '各区楼盘'
                },
            }
        ]
    }
]

const router = new VueRouter({
    routes
})

//路由守护
router.beforeEach((to, from, next) => {
    var token = localStorage.getItem('username')

    if (to.path == '/login') {//跳登录页不检测
        next()
    } else {
        if (token) {//如果有token 则允许跳转
            next()
        } else {//本地存储token被清除则跳转登录页
            Message.error('账号已过期，请您重新登录！')
            localStorage.clear()
            next('/login')
            return
        }
    }
})

export default router